package com.tjetc.dao.impl;

import com.tjetc.dao.NclassesDao;
import com.tjetc.domain.Nclasses;
import com.tjetc.domain.NurClasses;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class NclassesDaoImpl implements NclassesDao {
    @Override
    public void addNur(Nclasses nclasses) throws SQLException {
        Db.runner0().update("insert into nclasses(nid, date) values (?,?)",
                nclasses.getNid(),nclasses.getDate());
    }

    @Override
    public int getNurCount(int nid) throws SQLException {
        return ((Long)Db.runner0().query("select count(id) from nclasses where nid = ?",
                new ScalarHandler<>(),nid)).intValue();
    }

    @Override
    public List<NurClasses> listNurClasses(PageInfo<NurClasses> pageInfo) throws SQLException {
        return Db.runner0().query("select date,GROUP_CONCAT(nid) as nids from nclasses  GROUP BY date limit ?,?",
                new BeanListHandler<>(NurClasses.class),pageInfo.getStartRecord(),pageInfo.getPageSize());
    }

    @Override
    public int getCount() throws SQLException {
        return ((Long)Db.runner0().query("select count(id) from nclasses",
                new ScalarHandler<>())).intValue();
    }

    @Override
    public Nclasses findByIdandDate(int id, String dayOfWeekName) throws SQLException {
        return Db.runner0().query("select * from nclasses where nid=? and date=?",
                new BeanHandler<>(Nclasses.class),
                id,dayOfWeekName);
    }

    @Override
    public int setqiandao(Nclasses nclasses) throws SQLException {
        return Db.runner0().update("update nclasses set status=? where id=?",
                nclasses.getStatus(),nclasses.getId());
    }

    @Override
    public void clearNclasses() throws SQLException {
        Db.runner().update("delete from nclasses");
    }
}
